package com.cpd.blog.common.utils;

import com.cpd.blog.common.constant.Constant;
import com.cpd.blog.common.constant.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 日志工具类
 *
 * @Author: Copydown
 * @Date: 2022/05/01/1:29
 */
@Slf4j
@Component
public class LogUtil {

    @Value("${web.interceptor}")
    private String loginInterceptorPath;

    private static String path;

    @PostConstruct
    public void getPath() {
        path = this.loginInterceptorPath;
    }

    private static String getSuperName() {
        String className = new Throwable().getStackTrace()[2].getClassName();
        if (CheckUtil.isEquals(className, path)) {
            return className;
        }
        return new Throwable().getStackTrace()[3].getClassName();
    }

    public static void log(int code, String msg) {
        log.info(Constant.LOG_FORMAT, code, msg, getSuperName());
    }

    public static void log(StatusCode statusCode) {
        log(statusCode.getCode(), statusCode.getMsg());
    }

    public static void log(String format, Object... args) {
        log.info(format + Constant.LOG_PATH, args, getSuperName());
    }

}
